package com.cqut.forum.service.imp;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.cqut.forum.dao.MessagesDao;
import com.cqut.forum.dao.RelationDao;
import com.cqut.forum.dao.UserDao;
import com.cqut.forum.entity.Messages;
import com.cqut.forum.entity.Relation;
import com.cqut.forum.entity.User;
import com.cqut.forum.service.FriendRelationService;
import com.cqut.forum.util.MapKey;

/**
 * 好友关系service实现类
 * @author yangxun
 */
@Controller
public class FriendRelationServiceImp implements FriendRelationService {

	@Resource
	private UserDao userDao;
	@Resource
	private RelationDao relationDao;
	@Resource
	private MessagesDao messagesDao;
	@Override
	public Relation addFriendRelation(User user, String friendName, String level) {
		User friend = userDao.getUser(friendName);
		
		Relation relation = new Relation();
		relation.setFriendId(friend.getUserId());
		relation.setFriendRemarkName(friend.getUserName());
		relation.setIsCare(false);
		relation.setIsOnline(false);
		relation.setRelationTime(new Date());
		relation.setRelationLevel(level);
		relation.setUser(user);
		relationDao.save(relation);
		
		return relation;
	}
	@Override
	public Relation updateFriendRelation(int relationId) {
		return null;
	}
	@Override
	public Relation deleteRelation(int relationId,boolean isDeep) {
		Relation relation = relationDao.delete(relationId, Relation.class);

		if(isDeep){
			User user = relationDao.getUser(relation.getRelationId());
			//TODO深度删除好友给用户一条信息回馈
			Messages m = new Messages();
			m.setAboveMessagesId(-1);
			m.setAcceptUserId(relation.getFriendId());
			m.setMessagesContents("你的好友"+user.getUserName()+"已经与你解除了好友关系！");
			m.setMessagesState("未读");
			m.setMessagesTime(new Date());
			m.setLastReplyTime(m.getMessagesTime());
			m.setUser(user);

			relation = relationDao.getFriend(relation.getFriendId(),user.getUserId());
			relation = relationDao.delete(relation.getRelationId(), Relation.class);
			messagesDao.save(m);
		}
		return relation;
	}
	@Override
	public List<Map<String, Object>> getFriendsByUserId(int userId,int index,int size) {
		List<Map<String, Object>> friends = relationDao.getFriends(userId, index,size);
		return friends;
	}
	@Override
	public List<Map<String, Object>> loadUserByName(String userName) {
		List<Map<String, Object>> users = relationDao.loadUserByName(userName);
		return users;
	}
	@Override
	public Relation getById(int relatonId) {
		Relation r = relationDao.get(relatonId, Relation.class);
		return r;
	}
	@Override
	public boolean updateApply(User user,int relationId,boolean state) {
		Relation relation = relationDao.get(relationId, Relation.class);
		User friend = relationDao.getUser(relationId);
		if(state){
			relation.setRelationLevel("好友");
			relationDao.update(relation);
			
			addFriendRelation(user,friend.getUserName(),"好友");
		}
		else{
			relationDao.delete(relationId, Relation.class);
		}
		
		return true;
	}
	@Override
	public List<Map<String, Object>> loadApplys(int userId) {
		List<Map<String, Object>> applys = relationDao.loadByLevel(userId,"申请");
		
		return MapKey.changeMapKey(applys);
	}
	@Override
	public int countFriends(int userId) {
		int count = relationDao.countFriends(userId);
		return count;
	}
	@Override
	public Relation getRelation(int userId, int friendId) {
		Relation user = relationDao.getRelation(userId,friendId);
		return user;
	}
	@Override
	public boolean addCare(int userId, int friendId) {
		Relation relation = relationDao.getRelation(userId, friendId);
		relation.setIsCare(true);
		relationDao.update(relation);
		return true;
	}
}
